﻿using ConfExpert.Enums;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;

namespace ConfExpert.Forms.Rapor
{ 
    public partial class TelaBarkod : DevExpress.XtraReports.UI.XtraReport
    {
        private void Etiket_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }
        string MyRange;
        public TelaBarkod(EtiketTipi tip, string range)
        {
            InitializeComponent();

             MyRange = range;
            if (tip == EtiketTipi.RollId)
                MyRange = MakeCriteria(range);

            if (MyRange != String.Empty)
            {
                //Eras Light ITC - Century - Microsoft Sans Serif
                DataSet ds = Session.DataManager.ExecuteDataSet("GetBarcodesTela", tip, MyRange);
                
                myData1.xEtiketTela.Merge(ds.Tables[0]);
                //myData1.xEtiketTelaAlt.Merge(ds.Tables[1]);
                telaBarkodAlt1.SetData(ds.Tables[1]);
                if (tip==0)
                {
                    this.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
                }

            }

            xrTableCell24.BeforePrint += new PrintEventHandler(xrLabel_BeforePrint);
            xrTableCell14.BeforePrint += new PrintEventHandler(xrLabel_BeforePrint);

            this.PrintProgress += new DevExpress.XtraPrinting.PrintProgressEventHandler(this.Etiket_PrintProgress);
}

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        
        {
            Session.DataManager.ExecuteNonQuery("UpdTelaSet", MyRange);
        }
        

        private static string MakeCriteria(string range)
        {
            string[] parts = range.Replace(" ", String.Empty).Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
            string str = String.Empty;
            foreach (string part in parts)
            {
                try
                {
                    int loc = part.IndexOf("-");
                    if (loc > -1)
                    {
                        int p1 = int.Parse("0" + part.Substring(0, loc).Replace("-", ""));
                        int p2 = int.Parse("0" + part.Substring(loc, part.Length - loc).Replace("-", ""));
                        if (p1 == 0 && p2 > 0)
                            str += " OR r.RollId <= " + p2.ToString();
                        else if (p1 > 0 && p2 == 0)
                            str += " OR r.RollId >= " + p1.ToString();
                        else
                            str += " OR r.RollId between " + p1.ToString() + " and " + p2.ToString();
                    }
                    else
                        str += " OR r.RollId = " + int.Parse(part).ToString();
                }
                catch
                {
                }
            }
            return str.Substring(4);
        }

        private void xrLabel_BeforePrint(object sender, PrintEventArgs e)
        {
            int factor;

            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            if (this.ReportUnit == ReportUnit.HundredthsOfAnInch)
            {
                gr.PageUnit = GraphicsUnit.Inch;
                factor = 100;
            }
            else
            {
                gr.PageUnit = GraphicsUnit.Millimeter;
                factor = 10;
            }

            SizeF size = gr.MeasureString(((XRLabel)sender).Text, ((XRLabel)sender).Font);
            
            XRLabel senderLabel = (XRLabel)sender;

            if (senderLabel.Width < size.Width * factor)
                senderLabel.Font = new System.Drawing.Font(senderLabel.Font.FontFamily, senderLabel.Font.Size * senderLabel.Width / size.Width / factor * 1.025F);

            gr.Dispose();
        }

        private void xrSub_BeforePrint(object sender, PrintEventArgs e)
        {
            telaBarkodAlt1.prmRoll.Value = Convert.ToInt32(GetCurrentColumnValue("RollId"));
        }

        string lastProcessed = string.Empty;
        private void Etiket_PrintProgress(object sender, DevExpress.XtraPrinting.PrintProgressEventArgs e)
        {
            object x = GetCurrentRow();
            if (x != null)
            {
                System.Data.DataTable dt = ((System.Data.DataRowView)GetCurrentRow()).DataView.Table;
                string rollIds = string.Empty;
                foreach (System.Data.DataRow dr in dt.Rows)
                {
                    rollIds += dr["RollId"] + ",";
                }
                rollIds = rollIds.Substring(0, rollIds.Length - 1);

                if (lastProcessed != rollIds)
                {
                    Session.DataManager.ExecuteNonQuery("UpdOverProductionBarcodeSet", rollIds);
                    lastProcessed = rollIds;
                }
            }
        }

    }
}